package javasec.contempt;

import javasec.contempt.data.SeedConnector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.plugin.AbstractUIPlugin;


public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor
{

	public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer)
	{
		super(configurer);
	}

	public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer)
	{
		return new ApplicationActionBarAdvisor(configurer);
	}

	public void preWindowOpen()
	{
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(1000, 618));
		configurer.setShowCoolBar(false);
		configurer.setShowStatusLine(true);
		configurer.setTitle("Contempt");
	}
	
	public void postWindowOpen()
	{
		try
		{
			final Shell shell = getWindowConfigurer().getWindow().getShell();
			Display display=shell.getDisplay();
			Image image = AbstractUIPlugin.imageDescriptorFromPlugin("javasec.contempt","icons/contempt-tray-green.gif").createImage();
			Tray tray = display.getSystemTray();
			if(tray != null)
			{
				TrayItem trayItem = new TrayItem(tray, SWT.NONE);
				trayItem.setText("Contempt");
				trayItem.setToolTipText("Contempt");
				trayItem.setImage(image);

				// add the context menu
				final Menu menu = new Menu(shell, SWT.POP_UP);
				MenuItem exit = new MenuItem(menu, SWT.PUSH);
				exit.setText("Exit");
				exit.addListener(SWT.Selection, new Listener()
				{
					public void handleEvent(Event event)
					{
						getWindowConfigurer().getWorkbenchConfigurer().getWorkbench().close();
					}
				});
				
				trayItem.addListener(SWT.MenuDetect, new Listener()
				{
					public void handleEvent(Event event)
					{
						menu.setVisible(true);
					}
				});
				
				// add the click listener
				trayItem.addSelectionListener
				(
					new SelectionListener()
					{
						public void widgetDefaultSelected(SelectionEvent e)
						{	
						}

						public void widgetSelected(SelectionEvent e)
						{
							// if the window is minimized,
							// assume we are opening it
							boolean opening=shell.getMinimized();
							
							shell.setVisible(opening);
							if(opening)
							{
								shell.setActive();
								shell.setFocus();
							}
							shell.setMinimized(!opening);
						}
					}
				);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	public boolean preWindowShellClose()
	{
		getWindowConfigurer().getWindow().getShell().setVisible(false);
		return false;
	}
}
